package com.ncui.wisdomLearn.config;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.apache.commons.lang3.StringUtils;

/**
 * P6spy SQL 日志格式化
 *
 * @author ncui
 */
public class P6spyLogFormat implements MessageFormattingStrategy {

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category,
                                String prepared, String sql, String url) {
        return StringUtils.isNotEmpty(sql) ? formatSql(now, elapsed, category, connectionId, sql) : "";
    }

    private String formatSql(String now, long elapsed, String category, int connectionId, String sql) {
        StringBuilder sb = new StringBuilder()
                .append("\n=================================== SQL LOG ===================================")
                .append("\nSQL: ").append(sql.replaceAll("[\\s]+", " "))
                .append("\n==============")
                .append(" 耗时: ").append(elapsed).append("ms")
                .append(" | 分类: ").append(category)
                .append(" | 连接: Connection-").append(connectionId)
                .append(" ===============\n");
        return sb.toString();
    }
}